using System;
using System.Runtime.Serialization;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

using BLToolkit.Reflection;
using BLToolkit.TypeBuilder;

namespace EMan.Common.WCF
{
  [AttributeUsage( AttributeTargets.All, Inherited = false, AllowMultiple = true )]
  public class DynamicKnownTypeAttribute : Attribute, IOperationBehavior
  {
    private readonly Type _knownType;


    public DynamicKnownTypeAttribute( Type type )
    {
      _knownType = TypeAccessor.GetAccessor( type ).Type;
    }

    #region IOperationBehavior Members
    public void Validate( OperationDescription operationDescription )
    {
      if( !operationDescription.KnownTypes.Contains( _knownType ) )
        operationDescription.KnownTypes.Add( _knownType );
    }

    public void ApplyDispatchBehavior( OperationDescription operationDescription, DispatchOperation dispatchOperation ){}
    public void ApplyClientBehavior( OperationDescription operationDescription, ClientOperation clientOperation ){}
    public void AddBindingParameters( OperationDescription operationDescription, BindingParameterCollection bindingParameters ){}
    #endregion
  }
}